﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BL;

namespace PowerShell_Commander
{
    public partial class Snippet : Form
    {
        public bool IsNew = true;
        public string name = string.Empty;
        public Snippet()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Are you shure?","Confirm");
        }

        private void Snippet_Load(object sender, EventArgs e)
        {
            if (IsNew)
            {
                btnOK.Text = "Add";
            }
            else
            {
                btnOK.Text = "Save";
                txbTitle.Text = name;
                rtxbNewSnippet.Text = BusinessLogic.GetSnippet(name);
                
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txbTitle.Text) || string.IsNullOrEmpty(rtxbNewSnippet.Text))
            {
                MessageBox.Show("Must input title and text.", "Info");
                return;
            }

            string title = txbTitle.Text.Trim();
            string text = rtxbNewSnippet.Text.Trim();

            if (IsNew)
            {
               
                BusinessLogic.SaveSnippet(title, text);

                MessageBox.Show("Snippet added.", "Info");
                this.Close();
            }
            else
            {

                if (!title.Equals(name))
                {
                    BusinessLogic.DeleteSnippet(name);
                }

                BusinessLogic.SaveSnippet(title, text);

                MessageBox.Show("Snippet saved.", "Info");
                this.Close();
            }
            
        }
    }
}
